package cn.tedu.csmall.product.repo.impl;

import cn.tedu.csmall.product.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.repo.BrandServiceRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 程旭东
 */
@Slf4j
@Repository
public class BrandServiceRepositoryImpl implements BrandServiceRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public void save(BrandStandardVO brandStandardVO) {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        String key = getItemKey(brandStandardVO.getId());

        redisTemplate.opsForSet().add(getAllKeysKey(), key);
        valueOperations.set(key,brandStandardVO);
    }

    @Override
    public BrandStandardVO get(Long id) {
        ValueOperations<String, Serializable> ops = redisTemplate.opsForValue();
        Serializable serializable = ops.get(getItemKey(id));
        if(serializable != null){
            if(serializable instanceof BrandStandardVO){
                return (BrandStandardVO)serializable;
            }
        }
        return null;
    }

    @Override
    public void save(List<BrandListItemVO> brands) {
        String key = getListKey();
        ListOperations<String, Serializable> opsForList = redisTemplate.opsForList();

        redisTemplate.opsForSet().add(getAllKeysKey(), key);
        for(BrandListItemVO brand : brands){
            opsForList.rightPush(key,brand);
        }
    }

    @Override
    public List<BrandListItemVO> list(long start,long end) {
        String listKey = getListKey();
        ListOperations<String, Serializable> opsForList = redisTemplate.opsForList();
        List<Serializable> serializableList = opsForList.range(listKey, start, end);

        List<BrandListItemVO> listItemVOS = new ArrayList();
        for(Serializable serializable:serializableList){
            if(serializable!=null){
                if(serializable instanceof BrandListItemVO){
                    listItemVOS.add((BrandListItemVO)serializable);
                }
            }
        }

        return listItemVOS;
    }

    @Override
    public Long deleteAll() {
        Set<String> allKey = getAllKey();
        Long delete = redisTemplate.delete(allKey);

        return delete;
    }

    private String getItemKey(Long id){
        return BRAND_ITEM_KEY_PREFIX+id;
    }

    private String getListKey() {
        return BRAND_LIST_KEY;
    }

    private String getAllKeysKey(){
        return BRAND_ALL_KEYS_KEY;
    }

    private Set<String> getAllKey(){
        Set<Serializable> serializables = redisTemplate.opsForSet().members(getAllKeysKey());
        Set<String> sets = new HashSet<>();
        for(Serializable serializable : serializables){
            sets.add((String)serializable);
        }
        log.debug("allkeys的值是：{}"+sets);
        return sets;

    }

}
